package com.changpay.zywy.wight

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.util.AttributeSet
import android.view.View
import com.changpay.zywy.utils.DensityUtils

class NumberProgressView(
    /**
     * Contxt
     */
      context: Context, attrs: AttributeSet
) : View(context, attrs) {

    /**
     * 进度条画笔的宽度（dp）
     */
    private val paintProgressWidth = 10.0f
    /**
     * 文字百分比的字体大小（sp） 名字
     */
    private val paintTextSize = 13

    private var position = 0

    /**
     * 文字百分比的字体大小（sp） 数值
     */
    private val paintNumberTextSize = 12

    /**
     * 左侧已完成进度条的颜色
     */
    private val paintLeftColor = Color.parseColor("#FDD492")
    private val paintLeftColorTwo = Color.parseColor("#A5DBF7")

    /**
     * 右侧未完成进度条的颜色
     */
    private val paintRightColor =Color.parseColor("#FFF2F2F2")

    /**
     * 百分比文字的颜色
     */
    private val paintTextColor =  Color.parseColor("#825F03")

    /**
     * 主线程传过来进程 0 - 100
     */
    private var progress: Int = 0


    private var name: String = "小姐"

    private var number: String = ""


    /**
     * 得到自定义视图的宽度
     */
    private var viewWidth: Int = 0

    /**
     * 得到自定义视图的Y轴中心点
     */
    private var viewCenterY: Int = 0

    /**
     * 画左边已完成进度条的画笔
     */
    private val paintleft = Paint(Paint.ANTI_ALIAS_FLAG)

    /**
     * 画右边未完成进度条的画笔
     */
    private val paintRight = Paint(Paint.ANTI_ALIAS_FLAG)

    /**
     * 画中间的百分比文字的画笔
     */
    private val paintText = Paint()

    /**
     * 要画的文字的宽度
     */
    private var textWidth: Int = 0

    /**
     * 画文字时底部的坐标
     */
    private var textBottomY: Float = 0.toFloat()

    /**
     * 要画的名字文字的宽度
     */
    private var textNameWidth: Float = 0f


    /**
     * 画文字时底部的坐标
     */
    private var textNameBottomY: Float = 0.toFloat()


    /**
     * 包裹文字的矩形
     */
    private val rect = Rect()

    /**
     * 包裹左边名字的矩形
     */
    private val rectName = Rect()

    /**
     * 文字总共移动的长度（即从0%到100%文字左侧移动的长度）
     */
    private var totalMovedLength: Float = 0f

    init {
        // 构造器中初始化数据
        initData()
    }

    /**
     * 初始化数据
     */
    private fun initData() {

        //设置进度条画笔的宽度
        val paintProgressWidthPx = DensityUtils.dip2px(context, paintProgressWidth)

        //设置百分比文字的尺寸
        val paintTextSizePx = DensityUtils.sp2px(context, paintTextSize)

        // 已完成进度条画笔的属性
        paintleft.color = paintLeftColor
        paintleft.strokeWidth = paintProgressWidthPx.toFloat()
        paintleft.strokeCap = Paint.Cap.ROUND//使圆弧两头圆滑
        paintleft.style = Paint.Style.STROKE

        // 未完成进度条画笔的属性
        paintRight.color = paintRightColor
        paintRight.strokeWidth = paintProgressWidthPx.toFloat()
        paintRight.style = Paint.Style.STROKE
        paintRight.strokeCap = Paint.Cap.ROUND//使圆弧两头圆滑

        // 百分比文字画笔的属性
        paintText.color = paintTextColor
        paintText.textSize = paintTextSizePx.toFloat()
        paintText.isAntiAlias = true

    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        getWidthAndHeight()
    }

    /**
     * 得到视图等的高度宽度尺寸数据
     */
    private fun getWidthAndHeight() {


        //得到自定义视图的高度
        val viewHeight = measuredHeight
        viewWidth = measuredWidth
        viewCenterY = viewHeight / 2

        textBottomY = viewCenterY - DensityUtils.dip2px(context, 10f).toFloat()

        paintText.getTextBounds(name, 0, name.length, rectName)
        textNameWidth = rectName.width().toFloat()
//        textNameBottomY = (viewCenterY + rectName.height() / 2).toFloat()
        textNameBottomY = textBottomY
        totalMovedLength = (viewWidth - DensityUtils.dip2px(context, 30f)).toFloat()
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        //得到float型进度
        val progressFloat = progress / 100.0f

        //当前文字移动的长度
        val currentMovedLentgh = totalMovedLength * progressFloat

        paintText.color = paintTextColor
        canvas.drawText(name, DensityUtils.dip2px(context, 15f).toFloat(), textNameBottomY, paintText)

        //画右侧未完成的进度条，这个进度条的长度不是严格按照百分比来缩放的，因为文字的长度会变化，所以它的长度缩放比例也会变化

        canvas.drawLine(
            DensityUtils.dip2px(context, 15f).toFloat(),
            viewCenterY.toFloat(),
             totalMovedLength+DensityUtils.dip2px(context, 15f).toFloat(),
            viewCenterY.toFloat(),
            paintRight
        )
        //画左侧已经完成的进度条，长度为从Veiw左端到文字的左侧
        if (position.rem(2)==1)
        {
            paintleft.color = paintLeftColorTwo
        }
        canvas.drawLine(
            DensityUtils.dip2px(context, 15f).toFloat(),
            viewCenterY.toFloat(),
             currentMovedLentgh+DensityUtils.dip2px(context, 15f).toFloat(),
            viewCenterY.toFloat(),
            paintleft
        )

        //画文字(注意：文字要最后画，因为文字和进度条可能会有重合部分，所以要最后画文字，用文字盖住重合的部分)
        //得到包围文字的矩形的宽高
        paintText.getTextBounds(number, 0, number.length, rect)
        textWidth = rect.width()
        paintText.color = Color.parseColor("#FA8319")
        canvas.drawText(number,   textNameWidth  + DensityUtils.dip2px(context, 20f).toFloat()  , textBottomY, paintText)
    }

    /**
     * @param progress 外部传进来的当前进度
     */
    fun setData(progress: Int,name:String,number:String,position:Int) {
        this.progress = progress
        this.name = name
        this.number = number
        this.position = position
        invalidate()
    }
}